package cn.db101.view;

import cn.db101.dao.bookDao;
import cn.db101.dao.shelfDao;
import cn.db101.model.book;
import cn.db101.model.book_shelf;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

public class addbook_Frame{
	 JFrame jf=new JFrame() ;
	private JTextField nameText;
	private JTextField bnoText;
	private JTextField isbnText;
	private JTextField publisherText;
	private JTextField writerText;
	private JComboBox typeBox;
	private ArrayList<book_shelf> res;
	private JTextField yearText;
	private JTextField monthText;
	private JTextField dayText;
	/**
	 * Create the frame.
	 * @wbp.parser.entryPoint
	 */
	public void init() {
		jf.setTitle("\u6DFB\u52A0\u56FE\u4E66");
		jf.setBounds(100, 100, 743, 735);
		
		JPanel panel = new JPanel();
		jf.getContentPane().add(panel, BorderLayout.WEST);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(addbook_Frame.class.getResource("/img/table_head.jpg")));
		
		JPanel panel_1 = new JPanel();
		panel_1.setToolTipText("");
		panel_1.setForeground(new Color(0, 0, 0));
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u6DFB\u52A0图书信息", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 727, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 698, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(324, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 293, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 381, Short.MAX_VALUE)
					.addGap(7))
		);
		
		JLabel label = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		typeBox = new JComboBox();
		typeBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		setBookSheltInfro();
		JLabel label_1 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JLabel label_1_1 = new JLabel("\u56FE  \u4E66  \u53F7\uFF1A");
		label_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		nameText = new JTextField();
		nameText.setColumns(10);
		
		bnoText = new JTextField();
		bnoText.setColumns(10);
		
		JLabel label_2_1 = new JLabel("isbn    \u53F7\uFF1A");
		label_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		isbnText = new JTextField();
		isbnText.setColumns(10);
		
		JLabel label_2_2 = new JLabel("\u51FA  \u7248  \u793E\uFF1A");
		label_2_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		publisherText = new JTextField();
		publisherText.setColumns(10);
		
		JButton button = new JButton("\u786E\u8BA4");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addAction(e);
			}
		});
		button.setIcon(new ImageIcon(addbook_Frame.class.getResource("/icon/device-floppy\u4FDD\u5B58.png")));
		
		JLabel label_1_2 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
		label_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		writerText = new JTextField();
		writerText.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("\u51FA\u7248\u65E5\u671F\uFF1A");
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JLabel lblNewLabel_2 = new JLabel("\u5E74");
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		yearText = new JTextField();
		yearText.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("\u6708");
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		monthText = new JTextField();
		monthText.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("\u65E5");
		lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		dayText = new JTextField();
		dayText.setColumns(10);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(59)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(label)
									.addGap(18)
									.addComponent(typeBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(nameText, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(label_1_1, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(bnoText, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addGap(18)
									.addComponent(lblNewLabel_2)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(yearText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addGap(74)
									.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(label_2_2, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(publisherText))
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(label_2_1, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(isbnText, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(label_1_2, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
												.addGroup(gl_panel_1.createSequentialGroup()
													.addComponent(lblNewLabel_4)
													.addGap(18)
													.addComponent(dayText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addComponent(writerText)))))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addGap(18)
									.addComponent(lblNewLabel_3)
									.addGap(27)
									.addComponent(monthText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(291)
							.addComponent(button)))
					.addContainerGap(103, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(39)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(typeBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(isbnText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(35)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(publisherText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(47)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(bnoText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(writerText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(lblNewLabel_2)
						.addComponent(yearText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_3)
						.addComponent(monthText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_4)
						.addComponent(dayText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(68)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		panel_1.setLayout(gl_panel_1);
		panel.setLayout(gl_panel);
		//���ô����Ƿ�ɸı�
		jf.setResizable(false);
		//���ô���ɼ�
		jf.setVisible(true);	
		//����JFrame������ʾ
		jf.setLocationRelativeTo(null);

	}
	protected void addAction(ActionEvent e) {
		// TODO Auto-generated method stub
		String idS = bnoText.getText().toString();
		String typeS = typeBox.getSelectedItem().toString();
		String nameS = nameText.getText().toString();
		String writerS = writerText.getText().toString();
		String publisherS = publisherText.getText().toString();
		String isbnS = isbnText.getText().toString();
		String yearS = yearText.getText().toString();
		String monthS = monthText.getText().toString();
		String dayS = dayText.getText().toString();
		if(idS.equals("")||idS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		if(typeS.equals("")||typeS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		if(nameS.equals("")||nameS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		if(writerS.equals("")||writerS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		if(publisherS.equals("")||publisherS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		if(isbnS.equals("")||isbnS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		if(yearS.equals("")||yearS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		if(dayS.equals("")||dayS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		if(monthS.equals("")||monthS==null) {
			JOptionPane.showMessageDialog(null, "����д������Ϣ");
			return;
		}
		book newbook = new book();
		newbook.setBook_name(nameS);
		newbook.setWriter(writerS);
		newbook.setBook_id(idS);
		newbook.setPublisher(publisherS);
		newbook.setBook_status(0);
		newbook.setDel(0);
		for(book_shelf bs : res) {
			if(bs.getBooks_type().equals(typeS)) {
				newbook.setBook_shelf_no(bs.getBook_shelf_no());
				break;
			}
		}
		newbook.setIsbn(isbnS);
		
		int year = Integer.parseInt(yearS.trim());//ȥ���ո�  
        int month = Integer.parseInt(monthS.trim());  
        int day = Integer.parseInt(dayS.trim());  
        Calendar c = Calendar.getInstance();//��ȡһ������ʵ��  
        c.set(year, month-1, day);//�趨����������
        java.util.Date date1 = c.getTime();
        Date date = new Date(date1.getTime());
        newbook.setPubdate(date);
        
        int ok = new bookDao().addBookInfro(newbook);
        if(ok==1) {
        	JOptionPane.showMessageDialog(null, "�ɹ�����鼮��Ϣ");
        	return;
        } else {
        	JOptionPane.showMessageDialog(null, "����鼮��Ϣʧ�ܣ�����ϸ�˶���Ϣ");
        	return;
        }
	}
	protected void setBookSheltInfro() {
		res = new shelfDao().getAll();
		for(book_shelf b : res) {
			typeBox.addItem(b.getBooks_type());
		}
	}
	public static void main(String[] args) {
		new addbook_Frame().init();
	}
}